package com.example.win10.myapplication;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;

public class MainActivity extends Activity implements AMap.OnMapLongClickListener,PoiSearch.OnPoiSearchListener{
     MapView mMapView = null;
     private AMap aMap;
    MyLocationStyle myLocationStyle;
    Button btnSearch;
    EditText edtKey;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取地图控件引用
        mMapView = (MapView) findViewById(R.id.map);
        //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState)，创建地图
        aMap = mMapView.getMap();
        mMapView.onCreate(savedInstanceState);
        //初始化地图控制器对象



        CameraUpdate mCameraUpdate =
                    CameraUpdateFactory.newCameraPosition(new CameraPosition
                            (new LatLng(23.18321,113.353111),15,30,0));
            aMap.moveCamera(mCameraUpdate);
            aMap.setOnMapLongClickListener(this);
            initSearch();
    }

    private void initSearch() {
        btnSearch = findViewById(R.id.btn_search);
        edtKey = findViewById(R.id.edt_key);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String key = edtKey.getText().toString();
                PoiSearch.Query query = new PoiSearch.Query(key,"","020");
                query.setPageSize(10);
                query.setPageNum(0);

                PoiSearch poiSearch = new PoiSearch(MainActivity.this,query);
                poiSearch.setBound(new PoiSearch.SearchBound
                        (new LatLonPoint(23.18321,113.353111), 3000));
                poiSearch.setOnPoiSearchListener(MainActivity.this);
                poiSearch.searchPOIAsyn();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，销毁地图
        mMapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView.onResume ()，重新绘制加载地图
        mMapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView.onPause ()，暂停地图的绘制
        mMapView.onPause();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState)，保存地图当前的状态
        mMapView.onSaveInstanceState(outState);
    }

    @Override
    public void onMapLongClick(LatLng latLng) {
        Log.e("MainActivity","latitude"+
                latLng.latitude+"longtude="+latLng.longitude);
        Marker marker = aMap.addMarker(new MarkerOptions()
                .position(latLng).title("广东交通大学").snippet("Welcome"));

    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        Log.e("MainActivity","onPoiSearched");
        if(poiResult == null){
            return;
        }

        ArrayList<PoiItem> poiItemList = poiResult.getPois();
        for(PoiItem poiItem : poiItemList){
            Log.e("MainActivity",poiItem.getTitle());
            double latitude = poiItem.getLatLonPoint().getLatitude();
            double longitude = poiItem.getLatLonPoint().getLongitude();

            LatLng latLng = new LatLng(latitude,longitude);
            aMap.addMarker(new MarkerOptions()
                    .position(latLng).title(poiItem.getTitle()).snippet(poiItem.getTel()));
        }

    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }
}
